local skill = fk.CreateSkill{
  name = "tea__chouren"
}
local setZePile = function(player, cids, skillName)
  player:addToPile("$tea__ze_pile", cids, true, skillName, player.id)
end
---@type ActiveSkillSpec
local chouren = {
  name = "tea__chouren",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  expand_pile = function(self, player)
    return player:getPile("$tea__ze_pile")
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  can_use = function(self, player)
    local cards = player:getPile("$tea__ze_pile")
    if #cards < 5 then return false end
    local numberup, numberdown
    if table.every(cards, function(id)
      if not numberup then
        numberup = Fk:getCardById(id).number
        return true
      elseif (Fk:getCardById(id).number or 0) >= numberup then
        numberup = Fk:getCardById(id).number
        return true
      else
        return false
      end
    end) or
    table.every(cards, function(id)
      if not numberdown then 
        numberdown = Fk:getCardById(id).number
        return true
      elseif (Fk:getCardById(id).number or 0) <= numberdown then
        numberdown = Fk:getCardById(id).number
        return true
      else
        return false
      end
    end) then
      return true
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local cards = player:getPile("$tea__ze_pile")
    room:notifySkillInvoked(player, self.name, "offensive")
    player:broadcastSkillInvoke(skill.name, 1)
    player:showCards(cards)
    if not player.dead and not target.dead then
      room:damage{
        from = player,
        to = target,
        damage = 2,
        skillName = self.name,
      }
      local new_rets = {}
      local length = #cards
      for i = 1, length do
        local randomOne = table.random(cards, 1)
        table.insert(new_rets, randomOne)
        table.removeOne(cards, randomOne)
      end
      if #new_rets == 5 then
        setZePile(player, new_rets, self.name)
      end
    end
  end,  
}
local tableMerge = function(tableOne, tableTwo)
  local arr = {}
  if not tableOne then tableOne = {} end
  if not tableTwo then tableTwo = {} end
  for _, i in ipairs(tableOne) do
    table.insert(arr, i)
  end
  for _, i in ipairs(tableTwo) do
    table.insert(arr, i)
  end
  return arr
end
---@type TrigSkelSpec<TrigFunc>
local chouren_trigger = {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill("tea__chouren") and player:isWounded() then
      local room = player.room
      local targets = table.filter(
        room.alive_players, function(p)
          return table.every(room:getOtherPlayers(p), function(p2)
            return p.hp >= p2.hp
          end)
        end) or {}
      if #targets == 1 then
        event:setCostData(self, targets[1])
        return room:askToSkillInvoke(targets[1], {skill_name = chouren.name, prompt = "#tea__chouren_trigger-invoke:"..player.id})
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local from = event:getCostData(self)
    player:broadcastSkillInvoke(chouren.name, 2)
    room:notifySkillInvoked(player, chouren.name, "special")
    room:recover{
      who = player,
      num = 1,
      recoverBy = from,
      skillName = self.name
    }
    if not player.dead and not from.dead then
      local cards_data = {
        {"$tea__ze_pile", player:getPile("$tea__ze_pile")},
        {"$hand", from:getCardIds("h")},
        {"$equip", from:getCardIds("e")},
      }
      local choose_data = {
        to = nil,
        skillName = self.name,
        prompt = "#tea__chouren-choose",
      }
      local ret = room:askToPoxi(from, {
        poxi_type = "tea__chouren_swap",
        data = cards_data,
        extra_data = choose_data,
      })
      if #ret == 2 then
        local fromCard = table.filter(ret, function(id)
          return table.contains(from:getCardIds("he"), id)
        end)
        table.removeOne(ret, fromCard[1])
        if fromCard and ret then
          local zePile = player:getPile("$tea__ze_pile")
          local index = table.indexOf(zePile, ret[1])
          table.insert(zePile, index, fromCard[1])
          table.removeOne(zePile, ret[1])
          room:moveCardTo(tableMerge(ret, zePile), Card.Processing)
          setZePile(player, zePile, self.name)
          room:obtainCard(from, ret, false, fk.ReasonJustMove)
        end
      end
    end
  end,
}
Fk:addPoxiMethod{
  name = "tea__chouren_swap",
  card_filter = function(to_select, selected, data)
    if #selected == 0  then
      return true
    elseif #selected == 1 then
      local check = table.contains(data[1][2], selected[1]) and tableMerge(data[2][2], data[3][2]) or data[1][2]
      return table.contains(check, to_select)
    end
    return false
  end,
  feasible = function(selected)
    return #selected == 2
  end,
  prompt = function (data, extra_data)
    return extra_data.prompt
  end
}
skill:addEffect("active", chouren)
skill:addEffect(fk.RoundEnd, chouren_trigger)
return skill